In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
emissions = pd.read_csv(r"../Data/TotalCo2EmissionsByCountry.csv", index_col=0)
emissions.head()
Out[3]:
Year Afghanistan | AFG Albania | ALB Algeria | DZA American Samoa | ASM Andorra | AND Angola | AGO Antigua and Barbuda | ATG Arab World | ARB Argentina | ARG ... Uzbekistan | UZB Vanuatu | VUT Venezuela, RB | VEN Vietnam | VNM Virgin Islands (U.S.) | VIR West Bank and Gaza | PSE World | WLD Yemen, Rep. | YEM Zambia | ZMB Zimbabwe | ZWE
0 1960 414.371 2024.184 6160.560 NaN NaN 550.050 36.670 59563.98922 48815.104 ... NaN NaN 57069.521 7491.681 NaN NaN 9.396706e+06 3633.997 NaN NaN
1 1961 491.378 2280.874 6065.218 NaN NaN 454.708 47.671 65151.09581 51180.319 ... NaN NaN 51928.387 7986.726 NaN NaN 9.434403e+06 2665.909 NaN NaN
2 1962 689.396 2464.224 5669.182 NaN NaN 1180.774 102.676 74357.70773 53695.881 ... NaN 40.337 54106.585 9347.183 NaN NaN 9.818840e+06 3887.020 NaN NaN
3 1963 707.731 2082.856 5427.160 NaN NaN 1151.438 84.341 87895.97916 50083.886 ... NaN 33.003 56204.109 9119.829 NaN NaN 1.035575e+07 2918.932 NaN NaN
4 1964 839.743 2016.850 5650.847 NaN NaN 1224.778 91.675 103196.28160 55727.399 ... NaN 62.339 56603.812 11800.406 NaN NaN 1.094701e+07 3633.997 3278.298 4473.74

5 rows × 249 columns

In [16]:
def createStackedPropArea(df, time, cols, hover, title, yaxisTitle): 
    """
    A function which manipulates the data into the correct format to produce a stacked proportional area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text that you want to include on the hovertext
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    PCcols = []
    traces = []
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
        
    stackedAreaDF['Total'] = stackedAreaDF[cols].sum(axis =1)
    
    for col in cols:
        stackedAreaDF["pc_"+str(col)] = stackedAreaDF[col] / stackedAreaDF['Total']
        PCcols.append("pc_"+str(col))
        
    stackedPCAreaDF = stackedAreaDF[PCcols].cumsum(axis=1)
    stackedPCAreaDF[time] = stackedAreaDF[time]
    
    for col in PCcols:
        traces.append({'type' : 'scatter',
                      'x' : stackedPCAreaDF[time],
                      'y' : stackedPCAreaDF[col],
                      'name' : col[3:-6],
                      'mode' : 'lines',
                      'fill' : 'tonexty'})
    
    data = Data(traces)
    layout = {'title' : title,
             'xaxis' : {'title' : time},
             'yaxis' : {'title' : yaxisTitle}}
    fig = Figure(data=data, layout=layout)
    pyo.iplot(fig)

    
    return fig
    
test = createStackedPropArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND',], 'Proportion of total C02 Emissions: ',
                            "Proportion of Co2 Emissions, 1960-2011", 'Proprtion of Co2 Emissions')
/Users/josh/opt/anaconda3/lib/python3.9/site-packages/plotly/graph_objs/_deprecations.py:31: DeprecationWarning:

plotly.graph_objs.Data is deprecated.
Please replace it with a list or tuple of instances of the following types
  - plotly.graph_objs.Scatter
  - plotly.graph_objs.Bar
  - plotly.graph_objs.Area
  - plotly.graph_objs.Histogram
  - etc.


In [17]:
def createStackedPropArea(df, time, cols, hover, title, yaxisTitle): 
    """
    A function which manipulates the data into the correct format to produce a stacked proportional area plot with Plotly.
    
    Takes six arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text that you want to include on the hovertext
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    PCcols = []
    traces = []
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
        
    stackedAreaDF['Total'] = stackedAreaDF[cols].sum(axis =1)
    
    for col in cols:
        stackedAreaDF["pc_"+str(col)] = stackedAreaDF[col] / stackedAreaDF['Total']
        PCcols.append("pc_"+str(col))
        
    stackedPCAreaDF = stackedAreaDF[PCcols].cumsum(axis=1)
    
    stackedAreaData = stackedAreaDF[[time] + PCcols].merge(stackedPCAreaDF[PCcols], 
                                        left_index = True,
                                         right_index = True,
                                        suffixes = ('_o','_c'))
    
    for col in PCcols:
        traces.append({'type' : 'scatter',
                      'x' : stackedAreaData[time],
                       ## CHANGE THE COLUMN REFERENCE ##
                      'y' : stackedAreaData[col + "_c"],
                      'name' : col[3:-6],
                      'mode' : 'lines',
                      'fill' : 'tonexty'})
    
    data = Data(traces)
    layout = {'title' : title,
             'xaxis' : {'title' : time},
             'yaxis' : {'title' : yaxisTitle}}
    fig = Figure(data=data, layout=layout)
    
    return stackedAreaData
    
test = createStackedPropArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND',], 'Proportion of total C02 Emissions: ',
                            "Proportion of Co2 Emissions, 1960-2011", 'Proprtion of Co2 Emissions')
test.head()
Out[17]:
Year pc_United Arab Emirates | ARE_o pc_United Kingdom | GBR_o pc_United States | USA_o pc_China | CHN_o pc_India | IND_o pc_United Arab Emirates | ARE_c pc_United Kingdom | GBR_c pc_United States | USA_c pc_China | CHN_c pc_India | IND_c
0 1960 0.000003 0.133514 0.660532 0.178398 0.027553 0.000003 0.133517 0.794049 0.972447 1.0
1 1961 0.000003 0.141847 0.693776 0.132967 0.031408 0.000003 0.141850 0.835626 0.968592 1.0
2 1962 0.000004 0.142484 0.717318 0.105743 0.034451 0.000004 0.142488 0.859806 0.965549 1.0
3 1963 0.000005 0.139973 0.723073 0.101231 0.035718 0.000005 0.139978 0.863051 0.964282 1.0
4 1964 0.000004 0.136649 0.731366 0.098142 0.033839 0.000004 0.136654 0.868019 0.966161 1.0
In [14]:
def createStackedPropArea(df, time, cols, hover, title, yaxisTitle): 
    """
    A function which manipulates the data into the correct format to produce a stacked proportional area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text that you want to include on the hovertext
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    PCcols = []
    traces = []
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
        
    stackedAreaDF['Total'] = stackedAreaDF[cols].sum(axis =1)
    
    for col in cols:
        stackedAreaDF["pc_"+str(col)] = stackedAreaDF[col] / stackedAreaDF['Total']
        PCcols.append("pc_"+str(col))
        
    stackedPCAreaDF = stackedAreaDF[PCcols].cumsum(axis=1)
    stackedAreaData = stackedAreaDF[PCcols + [time]].merge(stackedPCAreaDF[PCcols], 
                                        left_index = True,
                                         right_index = True,
                                        suffixes = ('_o','_c'))

    for col in PCcols:     
        stackedAreaData[col + '_t'] = "<b>" + str(col)[3:-6]  + "</b><br>" + str(hover) + stackedAreaData[col + "_o"].apply(lambda x:
            "{:.0%}".format(x))
        
        
        traces.append({'type' : 'scatter',
                      'x' : stackedAreaData[time],
                      'y' : stackedAreaData[col + "_c"],
                      'name' : col[3:-6],
                      'mode' : 'lines',
                      'fill' : 'tonexty'})
    
    data = Data(traces)
    layout = {'title' : title,
             'xaxis' : {'title' : time},
             'yaxis' : {'title' : yaxisTitle}}
    fig = Figure(data=data, layout=layout)
    
    return stackedAreaData
    
test = createStackedPropArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND',], 'Proportion of total C02 Emissions: ',
                            "Proportion of Co2 Emissions, 1960-2011", 'Proprtion of Co2 Emissions')
test.head()
Out[14]:
pc_United Arab Emirates | ARE_o pc_United Kingdom | GBR_o pc_United States | USA_o pc_China | CHN_o pc_India | IND_o Year pc_United Arab Emirates | ARE_c pc_United Kingdom | GBR_c pc_United States | USA_c pc_China | CHN_c pc_India | IND_c pc_United Arab Emirates | ARE_t pc_United Kingdom | GBR_t pc_United States | USA_t pc_China | CHN_t pc_India | IND_t
0 0.000003 0.133514 0.660532 0.178398 0.027553 1960 0.000003 0.133517 0.794049 0.972447 1.0 <b>United Arab Emirates</b><br>Proportion of t... <b>United Kingdom</b><br>Proportion of total C... <b>United States</b><br>Proportion of total C0... <b>China</b><br>Proportion of total C02 Emissi... <b>India</b><br>Proportion of total C02 Emissi...
1 0.000003 0.141847 0.693776 0.132967 0.031408 1961 0.000003 0.141850 0.835626 0.968592 1.0 <b>United Arab Emirates</b><br>Proportion of t... <b>United Kingdom</b><br>Proportion of total C... <b>United States</b><br>Proportion of total C0... <b>China</b><br>Proportion of total C02 Emissi... <b>India</b><br>Proportion of total C02 Emissi...
2 0.000004 0.142484 0.717318 0.105743 0.034451 1962 0.000004 0.142488 0.859806 0.965549 1.0 <b>United Arab Emirates</b><br>Proportion of t... <b>United Kingdom</b><br>Proportion of total C... <b>United States</b><br>Proportion of total C0... <b>China</b><br>Proportion of total C02 Emissi... <b>India</b><br>Proportion of total C02 Emissi...
3 0.000005 0.139973 0.723073 0.101231 0.035718 1963 0.000005 0.139978 0.863051 0.964282 1.0 <b>United Arab Emirates</b><br>Proportion of t... <b>United Kingdom</b><br>Proportion of total C... <b>United States</b><br>Proportion of total C0... <b>China</b><br>Proportion of total C02 Emissi... <b>India</b><br>Proportion of total C02 Emissi...
4 0.000004 0.136649 0.731366 0.098142 0.033839 1964 0.000004 0.136654 0.868019 0.966161 1.0 <b>United Arab Emirates</b><br>Proportion of t... <b>United Kingdom</b><br>Proportion of total C... <b>United States</b><br>Proportion of total C0... <b>China</b><br>Proportion of total C02 Emissi... <b>India</b><br>Proportion of total C02 Emissi...
In [13]:
def createStackedPropArea(df, time, cols, hover, title, yaxisTitle): 
    """
    A function which manipulates the data into the correct format to produce a stacked proportional area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text that you want to include on the hovertext
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    PCcols = []
    traces = []
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
        
    stackedAreaDF['Total'] = stackedAreaDF[cols].sum(axis =1)
    
    for col in cols:
        stackedAreaDF["pc_"+str(col)] = stackedAreaDF[col] / stackedAreaDF['Total']
        PCcols.append("pc_"+str(col))
        
    stackedPCAreaDF = stackedAreaDF[PCcols].cumsum(axis=1)
    stackedAreaData = stackedAreaDF[PCcols + [time]].merge(stackedPCAreaDF[PCcols], 
                                        left_index = True,
                                         right_index = True,
                                        suffixes = ('_o','_c'))

    for col in PCcols:       
        stackedAreaData[col + '_t'] = "<b>" + str(col)[3:-6]  + "</b><br>" + str(hover) + stackedAreaData[col + "_o"].apply(lambda x:
            "{:.0%}".format(x))
        
        
        traces.append({'type' : 'scatter',
                      'x' : stackedAreaData[time],
                      'y' : stackedAreaData[col + "_c"],
                       'text' : stackedAreaData[col + "_t"],
                       'hoverinfo' : 'text+x',
                      'name' : col[3:-6],
                      'mode' : 'lines',
                      'fill' : 'tonexty'})
    
    data = Data(traces)
    layout = {'title' : title,
             'xaxis' : {'title' : time},
             'yaxis' : {'title' : yaxisTitle,
                       'tickformat' : '%'},
              'hovermode' : 'closest'}
    fig = Figure(data=data, layout=layout)
    pyo.iplot(fig)

    return fig
    
    
C02Prop = createStackedPropArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND',], 'Proportion of total C02 Emissions: ',
                            "Proportion of Co2 Emissions, 1960-2011", 'Proprtion of Co2 Emissions')
In [ ]: